const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const movieSchema = new Schema({
    doubanId: {
        unique: true,
        type: String
    },
    category: [{
        type: Schema.Types.ObjectId,
        ref: 'Category'
    }],
    rate: Number,
    title: String,
    summary: String,
    video: String,
    poster: String,
    cover: String,
    // videoKey: String,
    // posterKey: String,
    // coverKey: String,
    rawTitle: String,
    movieTypes: [String],
    pubdate: Schema.Types.Mixed,
    year: Number,
    tags: Array,

    meta: {
        createAt: {
            type: Date,
            default: Date.now()
        },
        updateAt: {
            type: Date,
            default: Date.now()
        }
    }
})

// 完成对单条数据save之前中间件的操作
movieSchema.pre('save', function (next) {
    if (this.isNew) {
        this.meta.createAt = this.meta.updateAt = Date.now();
    } else {
        this.meta.updateAt = Date.now();
    }
    next();
})

mongoose.model('Movie', movieSchema);